package com.xzm.project.ui.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;

import com.xzm.project.R;

import androidx.annotation.Nullable;
import cn.wuyu.baselibrary.utils.DeviceUtil;

/**
 * https://mp.weixin.qq.com/s?__biz=MzIxOTU1MDg5Ng==&mid=2247484222&idx=1&sn=95f3793ed034027363773acfbf15fd1e&chksm=97d8c6e1a0af4ff797babbe78ee859f897cf4fec688d28e97adfef9ebd74bcb14a19cd12252e#rd
 */
public class CircleBarView extends View {
    private Paint rPaint; //正方形
    private Paint progressPaint;
    private CircleBarAnim anim;
    private Paint bgPaint;//绘制背景圆弧的画笔
    private Paint textPaint;//文本画笔
    private RectF mRectF;//绘制圆弧的矩形区域
    private float barWidth;//圆弧进度条宽度
    private int defaultSize;//自定义View默认的宽高
    private float progressNum;//可以更新的进度条数值
    private float maxNum;//进度条最大值

    private float progressSweepAngle;//进度条圆弧扫过的角度
    private float startAngle;//背景圆弧的起始角度
    private float sweepAngle;//背景圆弧扫过的角度



    private int progressColor;//进度条圆弧颜色
    private int bgColor;//背景圆弧颜色
    public CircleBarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {

        defaultSize = DeviceUtil.dp2px(context,100);
        barWidth = DeviceUtil.dp2px(context,10);
        mRectF = new RectF();

        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.CircleBarView);

        progressColor = typedArray.getColor(R.styleable.CircleBarView_progress_color,Color.GREEN);//默认为绿色
        bgColor = typedArray.getColor(R.styleable.CircleBarView_bg_color,Color.GRAY);//默认为灰色
        startAngle = typedArray.getFloat(R.styleable.CircleBarView_start_angle,0);//默认为0
        sweepAngle = typedArray.getFloat(R.styleable.CircleBarView_sweep_angle,360);//默认为360
        barWidth = typedArray.getDimension(R.styleable.CircleBarView_bar_width,DeviceUtil.dp2px(context,10));//默认为10dp
        typedArray.recycle();//typedArray用完之后需要回收，防止内存泄漏

        rPaint = new Paint();
        rPaint.setStyle(Paint.Style.STROKE);
        rPaint.setColor(Color.RED);
        rPaint.setStrokeWidth(2);

        textPaint = new Paint();
        textPaint.setStyle(Paint.Style.STROKE);//只描边，不填充
        textPaint.setColor(Color.BLACK);
        textPaint.setAntiAlias(true);//设置抗锯齿
//        textPaint.setStrokeWidth(16);
        textPaint.setTextSize(15);
        progressPaint = new Paint();
        progressPaint.setStyle(Paint.Style.STROKE);//只描边，不填充
        progressPaint.setColor(progressColor);
        progressPaint.setAntiAlias(true);//设置抗锯齿
        progressPaint.setStrokeWidth(barWidth);//随便设置一个画笔宽度，看看效果就好，之后会通过attr自定义属性进行设置
        anim = new CircleBarAnim();


        bgPaint = new Paint();
        bgPaint.setStyle(Paint.Style.STROKE);//只描边，不填充
        bgPaint.setColor(bgColor);
        bgPaint.setAntiAlias(true);//设置抗锯齿
        bgPaint.setStrokeWidth(barWidth);


        progressNum = 0;
        maxNum = 100;//也是随便设的

        startAngle = 0;
        sweepAngle = 360;

    }

    public class CircleBarAnim extends Animation {

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            super.applyTransformation(interpolatedTime, t);
//            sweepAngle = interpolatedTime * 360;
//           Logger.d("偏移角度==interpolatedTime=="+ interpolatedTime);
//           Logger.d("偏移角度==Transformation=="+ t.toString());

            progressSweepAngle = interpolatedTime * sweepAngle * progressNum / maxNum;//这里计算进度条的比例
            postInvalidate();
        }
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int height = measureSize(defaultSize, heightMeasureSpec);
        int width = measureSize(defaultSize, widthMeasureSpec);
        int min = Math.min(width, height);// 获取View最短边的长度
        setMeasuredDimension(min, min);// 强制改View为以最短边为长度的正方形


        if (min>barWidth*2){
            mRectF.set(barWidth/2,barWidth/2,min-barWidth/2,min-barWidth/2);
        }
    }
    private int measureSize(int defaultSize,int measureSpec) {
        int result = defaultSize;
        int specMode = View.MeasureSpec.getMode(measureSpec);
        int specSize = View.MeasureSpec.getSize(measureSpec);
        if (specMode == View.MeasureSpec.EXACTLY) {
            result = specSize;
        } else if (specMode == View.MeasureSpec.AT_MOST) {
            result = Math.min(result, specSize);
        }
        return result;
    }
    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float x = 50;
        float y = 50;
//        RectF rectF = new RectF(x, y, x + 300, y + 300); ///建一个大小为300 * 300的正方形区域

//        canvas.drawArc(rectF,0,sweepAngle,false,progressPaint); //这里角度0对应的是三点钟方向，顺时针方向递增
//        canvas.drawArc(mRectF, startAngle, sweepAngle, false, bgPaint);
//        canvas.drawArc(mRectF, startAngle, progressSweepAngle, false, progressPaint);
//        canvas.drawText("进度", (x + 300) / 2, (y + 300) / 2, textPaint);
//        canvas.drawRect(rectF,rPaint);

        canvas.drawArc(mRectF,startAngle,sweepAngle,false,bgPaint);
        canvas.drawArc(mRectF,startAngle,progressSweepAngle,false, progressPaint);
    }

    public void setProgressNum(float progressNum, int time) {
        this.progressNum = progressNum;
        anim.setDuration(time);
        this.startAnimation(anim);
    }
}
